# 统计文章里各个单词出现的次数，还有最多的前几个单词
word_counts = {}


def word_count():
    with open("014.txt", "r", encoding="utf-8") as file:
        for line in file:
            line = line[:-2]
            words = line.split()
            for word in words:
                # print(word)
                # 判断没有在列表的同时，得直接记录1次
                if word in word_counts:
                    word_counts[word] += 1
                else:
                    word_counts[word] = 1
    return word_counts


a = word_count()
print(a)
print(
    sorted(a.items(),
           key=lambda x: x[1],
           reverse=True
           )[:10]
)
